package com.itheima.service.impl;


import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.DateUtils;
import com.itheima.mapper.UserMemberMapper;
import com.itheima.mapper.UserOrderMapper;
import com.itheima.mapper.UserOrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.UserOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserOrderServiceImpl implements UserOrderService {
    @Autowired
    private UserOrderMapper userOrderMapper;
    @Autowired
    private UserOrderSettingMapper userOrderSettingMapper;
    @Autowired
    private UserMemberMapper userMemberMapper;

    /**
     * 根据预约id查询预约信息
     */
    public Order  findById(Integer id) {
        return userOrderMapper.findById(id);
    }



    /**
     * 体检预约
     */
    public Result order(Map map)throws Exception {
        //先检查用户选择的预约日期是否已经提前进行了预约设置  没有则无法预约
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting = userOrderSettingMapper.findByOrderDate(DateUtils.parseString2Date(orderDate));
        if(orderSetting==null ){
            //没有进行设置 返回数据
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER );
        }
        //判断是否已预约满
        if(orderSetting.getReservations()>=orderSetting.getNumber()){
            //预约已满
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //检查用户是否重复预约
        String telephone = (String) map.get("telephone"); //获取用户手机号
        Member  member = userMemberMapper.findByTelephone(telephone);
        if(member!=null){
            //判断是否在重复预约
            Integer memberId = member.getId();//会员id
            Date orderDate1 = DateUtils.parseString2Date(orderDate);//预约日期
            String setmealId = (String) map.get("setmealId");//套餐id
            Order order = new Order(memberId,orderDate1,Integer.parseInt(setmealId));
            //根据条件进行查询
            List<Order> list = userOrderMapper.findByCondition(order);
            if(list!=null && list.size()>0){
                //用户重复预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else{
            //检查当前用户是否为会员 如果是会员则直接完成注册 如果不是会员则自动完成注册
            member = new Member();//会员对象
            member.setPhoneNumber(telephone); //手机号
            member.setRegTime(new Date());//注册时间
            member.setIdCard((String)map.get("idCard") );//身份证号
            member.setName((String)map.get("name") );//姓名
            member.setSex((String)map.get("sex") );//性别
            //自动完成会员注册
            userMemberMapper.add(member);
        }
        //预约成功 更新当前预约人数
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员id
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//设置预约日期
        order.setOrderType((String)map.get("orderType") );//设置预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO) ;//到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//设置套餐id
        userOrderMapper.add(order);//添加预约信息
        //更新可预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        userOrderSettingMapper.editNumberByOrderDate(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order);
    }
}
